package com.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import com.tools.SomeMonitorEditText;

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ForgetPwd2Activity extends AppCompatActivity {

    private TextView 标题;
    private TextView 用户名;
    private ImageButton 后退按钮;
    private EditText 编辑新密码;
    private EditText 再次输入密码;
    private Button 提交;

    String userName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpwd);

        标题 = (TextView) findViewById(R.id.标题);
        用户名 = (TextView) findViewById(R.id.用户名);
        后退按钮 = (ImageButton) findViewById(R.id.后退按钮);
        编辑新密码 = (EditText) findViewById(R.id.编辑新密码);
        再次输入密码 = (EditText) findViewById(R.id.再次输入密码);
        提交 = (Button) findViewById(R.id.提交);

        //通过传来的手机号获得用户名
        String phone = getIntent().getStringExtra("phone");
        String path1 = "http://101.200.40.47:8080/stu/user/getUserNameByPhone?phone="+phone;
        new getUserName().execute(path1);

        标题.setText("重置密码");

        new SomeMonitorEditText().SetMonitorEditText(提交,编辑新密码,再次输入密码);

        后退按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ForgetPwd2Activity.this, ForgetPwd1Activity.class);
                startActivity(intent);
                ForgetPwd2Activity.this.finish();
            }
        });

        提交.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = 编辑新密码.getText().toString().trim();
                String rpwd = 再次输入密码.getText().toString().trim();
                if(!pwd.equals(rpwd)){
                    Toast.makeText(ForgetPwd2Activity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                    再次输入密码.setText(null);
                }else{
                    String path2 = "http://101.200.40.47:8080/stu/user/doResetPassword?userName="+userName+"&password="+pwd;
                    new doResetPassword().execute(path2);
                    Intent intent = new Intent();
                    intent.setClass(ForgetPwd2Activity.this, LoginActivity.class);
                    startActivity(intent);
                    ForgetPwd2Activity.this.finish();
                }

            }
        });

    }

    //获取用户名
    private class getUserName extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            String path = params[0].toString();

            try {
                //以get方式提交整个路径
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//这个应该都知道是干嘛的，默认是GET  
                conn.setConnectTimeout(5000);

                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str = br.readLine();
                    return str;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if(o==null){
                Toast.makeText(ForgetPwd2Activity.this, "未知错误！返回登录页面！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(ForgetPwd2Activity.this,LoginActivity.class);
                startActivity(intent);
                ForgetPwd2Activity.this.finish();
            }else{
                userName = o.toString().trim().substring(1,o.toString().trim().length()-1);
                用户名.setText(userName);
            }
        }
    }

    //获取用户名
    private class doResetPassword extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            String path = params[0].toString();

            try {
                //以get方式提交整个路径
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//这个应该都知道是干嘛的，默认是GET  
                conn.setConnectTimeout(5000);

                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str = br.readLine();
                    return str;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if(o.toString().trim().equals("1")){
                Toast.makeText(ForgetPwd2Activity.this, "重置密码成功！", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(ForgetPwd2Activity.this, "未知错误！返回登录页面！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(ForgetPwd2Activity.this,LoginActivity.class);
                startActivity(intent);
                ForgetPwd2Activity.this.finish();
            }
        }
    }
    long mExitTime;       //实现“再按一次退出”的记录时间变量
    @Override //再按一次退出程序
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime < 2000) {
            super.onBackPressed();
        } else {
            mExitTime = System.currentTimeMillis();
            Toast.makeText(ForgetPwd2Activity.this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
        }
    }
}
